﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace DynamicRequestWebservice.Utils
{
    /// <summary>
    /// 错误日志
    /// </summary>
    public class LogHelper
    { 
        private static object lockFlag = new object();
        private static string LOG_FILE_PATH = AppDomain.CurrentDomain.BaseDirectory + "Log\\PortalLog";
        private static string LOG_AnalyzerFile_Path = AppDomain.CurrentDomain.BaseDirectory + "Log\\AnalyzerLog";

        /// <summary>
        /// 打印业务库异常日志
        /// </summary>
        /// <param name="ex"></param>
        public static void LogError(Exception ex)
        {
            LogInfo(String.Format("ExType:{0}\r\nExMessage:{1}\r\nExSource:{2}\r\nExStack:\r\n{3}\r\n",
                ex.GetType(), ex.Message, ex.Source, ex.StackTrace));
        }

        /// <summary>
        /// 打印Analyzer库异常日志
        /// </summary>
        /// <param name="ex"></param>
        public static void LogAnalyzerException(Exception ex)
        {
            var fileName = LOG_AnalyzerFile_Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            var message = String.Format("========================{0}=========================\r\n", DateTime.Now);
            message += String.Format("ExType:\r\n\t{0} \r\nExMessage:\r\n\t{1} \r\nExSource:\r\n\t{2} \r\nExStack:\r\n{3}\r\n", 
                ex.GetType(), 
                ex.Message,
                ex.Source,
                ex.StackTrace);
            message += "\r\n\r\n\r\n";

            lock (lockFlag)
            {
                if (!Directory.Exists(LOG_AnalyzerFile_Path))
                {
                    Directory.CreateDirectory(LOG_AnalyzerFile_Path);
                }

                File.AppendAllText(fileName, message);
            }
        }

        /// <summary>
        /// 打印业务库信息
        /// </summary>
        /// <param name="msg"></param>
        public static void LogInfo(string msg)
        {
            lock (lockFlag)
            {
                if (!Directory.Exists(LOG_FILE_PATH))
                {
                    Directory.CreateDirectory(LOG_FILE_PATH);
                }
                string fileName = LOG_FILE_PATH + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                msg = String.Format("------------------------{0}------------------------------\r\n", DateTime.Now) + msg;
                msg += "\r\n\r\n\r\n";
                File.AppendAllText(fileName, msg);
            }
        }

        /// <summary>
        /// 打印Analyzer信息
        /// </summary>
        /// <param name="msg"></param>
        public static void LogAnalyzerInfo(string msg)
        {
            var fileName = LOG_AnalyzerFile_Path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            var message = String.Format("========================{0}=========================\r\n", DateTime.Now);
            message += String.Format("PrintInfo:\r\n\t{0}", msg);
            message += "\r\n\r\n\r\n";
            lock (lockFlag)
            {
                if (!Directory.Exists(LOG_AnalyzerFile_Path))
                {
                    Directory.CreateDirectory(LOG_AnalyzerFile_Path);
                }
                File.AppendAllText(fileName, message);
            }
        }

        [Obsolete]
        public static void ErrorLogInfo(string msg)
        {
            StreamWriter streamWriter = null;
            try
            {
                lock (lockFlag)
                {
                    if (!Directory.Exists(LOG_FILE_PATH))
                    {
                        Directory.CreateDirectory(LOG_FILE_PATH);
                    }
                    string fileName = LOG_FILE_PATH + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    if (streamWriter == null)
                    {
                        streamWriter = !File.Exists(fileName) ? File.CreateText(fileName) : File.AppendText(fileName);
                    }
                    streamWriter.WriteLine("***********************************************************************");
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                    streamWriter.WriteLine("输出信息：错误信息");
                    if (!String.IsNullOrEmpty(msg))
                    {
                        streamWriter.WriteLine("异常信息：\r\n" + msg);
                    }
                }
            }
            catch (Exception ex)
            {
                //
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Flush();
                    streamWriter.Dispose();
                    streamWriter = null;
                }
            }
        }
    }
}